﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Framework.AssetsManager;
namespace Framework
{
    public class SceneLoader : MonoBehaviour
    {
        //public Slider slider;


        void Awake()
        {
            if (SceneManager.Instance.sceneNames.Count == 0)
                return;
            
            Mainloop.Instance.StartCoroutine(LoadScene());
        }

        IEnumerator LoadScene()
        {
            int sceneNum = SceneManager.Instance.sceneNames.Count;
            for (int i = 0; i < sceneNum; i++)
            {
                AsyncOperation loadOperation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(SceneManager.Instance.sceneNames[i], LoadSceneMode.Additive);
                while(!loadOperation.isDone)
                {
                    //slider.value = loadOperation.progress;
                     yield return new WaitForEndOfFrame();
                }
            }
            yield return new WaitForEndOfFrame();
            //slider.value = 1;

            Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetSceneByName(SceneManager.Instance.sceneNames[0]);
            UnityEngine.SceneManagement.SceneManager.SetActiveScene(activeScene);
            if (SceneManager.Instance.onLoadFinish != null)
                SceneManager.Instance.onLoadFinish();
           
        }
    }
}

